/******************************************************************************
January 13, 2017
Sui-Jade Ho
Dimitrije Ruzic

Input(s): Manufacturing_1977_2001.dta

This .do file has 1 section
SECTION 1:
	- Combines the production function parameters from FOC, OLS, and IV
		estimation
	- Averages parameters to construct a single production-function across
		years within each industry

******************************************************************************/

clear programs
clear all
set more off
set matsize 11000

local data "/FILE PATH GOES HERE/"

/*SECTION 1: Compiling Parameters*/
local S1 = 0
local S2 = 0
/*SECTION 3: New File with ACF Estimates*/
local S3 = 0
/*SECTION 4: New File with ACF Estimates, whole period and 5-year intervals*/
local S4 = 0
/*SECTION 5: Gross Output Parameters*/
local S5 = 0

/*********************************************************
SECTION 1: 
	- Construct (BLS) ECEC benefits factors with correct industry codes
	- Construct labor output elasticities
**********************************************************/
if `S1' == 1 {
import excel using "`data'ECEC_test.xls", sheet("Sheet1") firstrow allstring
destring value year, replace
rename value ecec_factor

preserve
keep if year == 1987
gen ecec_code = substr(code,1,2)
destring ecec_code, replace
keep ecec_code ecec_factor
tempfile o1987
	save "`o1987'"
restore

preserve
keep if year == 1992
gen ecec_code = substr(code,1,2)
destring ecec_code, replace
keep ecec_code ecec_factor
tempfile o1992
	save "`o1992'"
restore
 
preserve
keep if year == 1997
gen ecec_code = substr(code,1,2)
destring ecec_code, replace
keep ecec_code ecec_factor
tempfile o1997
	save "`o1997'"
restore

preserve
keep if year == 2002
gen ecec_code = substr(code,1,3)
destring ecec_code, replace
keep ecec_code ecec_factor
tempfile o2002
	save "`o2002'"
restore

preserve
keep if year == 2007
gen ecec_code = substr(code,1,3)
destring ecec_code, replace
keep ecec_code ecec_factor
tempfile o2007
	save "`o2007'"
restore


foreach yyyy of numlist 1977/1987 {
	preserve
	use "`o1987'", clear
	gen year = `yyyy'
	mkmat year ecec_code ecec_factor, mat(y`yyyy')
	restore
	}
	
foreach yyyy of numlist 1988/1992 {
	preserve
	use "`o1992'", clear
	gen year = `yyyy'
	mkmat year ecec_code ecec_factor, mat(y`yyyy')
	restore
	}
	
foreach yyyy of numlist 1993/1997 {
	preserve
	use "`o1997'", clear
	gen year = `yyyy'
	mkmat year ecec_code ecec_factor, mat(y`yyyy')
	restore
	}

foreach yyyy of numlist 1998/2002 {
	preserve
	use "`o2002'", clear
	gen year = `yyyy'
	mkmat year ecec_code ecec_factor, mat(y`yyyy')
	restore
	}
	
foreach yyyy of numlist 2003/2007 {
	preserve
	use "`o2007'", clear
	gen year = `yyyy'
	mkmat year ecec_code ecec_factor, mat(y`yyyy')
	restore
	}

mat final = y1977
foreach yyyy of numlist 1978/2007 {
	mat final = [final \ y`yyyy']
	}

drop _all
svmat final
rename final1 year
rename final2 ecec_code
rename final3 ecec_factor

save "`data'ECEC_Benefits_Factors.dta", replace

!gunzip /FILE PATH GOES HERE/Manufacturing_1977_2009.dta.gz
use "`data'Manufacturing_1977_2009.dta", clear
*!gzip /FILE PATH GOES HERE/Manufacturing_1977_2009.dta
drop if year>2007

keep ind_cmp dind year et sw wt PY2 fk_naics02 naics4 lbdnum hind

/*Mapping in the ECEC factors*/
gen ind1 = ind_cmp
gen ind2 = dind
tostring ind1 ind2, replace

gen ecec_code = substr(ind1,1,2) if year<1997
	replace ecec_code = substr(ind2,1,2) if year==1997
	replace ecec_code = substr(ind1,1,3) if year>1997 & year<=2007
destring ecec_code, replace
drop ind1 ind2

merge m:1 year ecec_code using "`data'ECEC_Benefits_Factors.dta"
drop _m

/*Keep ASM Only*/
keep if et==0

/*Labor Share of VA, FK6*/
bys fk_naics02 year: egen tempL = sum(sw*wt*ecec_factor)
bys fk_naics02 year: egen tempPY2 = sum(PY2*wt)

gen betaL_fk6 = tempL/(tempPY2)

cap label var betal_fk6 "Labor Share of VA, FK6"
drop tempL tempPY2

/*Labor Share of VA, FK4*/
bys naics4 year: egen tempL = sum(sw*wt*ecec_factor)
bys naics4 year: egen tempPY2 = sum(PY2*wt)

gen betaL_fk4 = tempL/(tempPY2)

cap label var betal_fk4 "Labor Share of VA, FK4"
drop tempL tempPY2

preserve
collapse betaL_fk6, by(fk year)
sort fk year
save "`data'PF_FK6_Year_BetaL.dta", replace
restore

preserve
collapse betaL_fk4, by(naics4 year)
sort naics4 year
save "`data'PF_FK4_Year_BetaL.dta", replace
restore


}

/*********************************************************
SECTION 2: 
	- Preparing labor revenue elasticities for ACF
**********************************************************/
if `S2' == 1 {

/************************************************************
FK6: Import labor elasticities with BLS benefits adjustments
*************************************************************/

use "`data'PF_FK6_Year_BetaL.dta", clear

gen period = 1 if year<=1982
	replace period = 2 if year>1982 & year<=1987
	replace period = 3 if year>1987 & year<=1992
	replace period = 4 if year>1992 & year<=1997
	replace period = 5 if year>1997 & year<=2002
	replace period = 6 if year>2002 & year<=2007

gen dec = 1 if year<=1987	
	replace dec = 2 if year>1987 & year<=1997
	replace dec = 3 if year>1997 & year<=2007
	
bys fk period: egen L_elasticity_BLS_5y = mean(betaL_fk6)
bys fk dec: egen L_elasticity_BLS_10y = mean(betaL_fk6)
bys fk: egen L_elasticity_BLS_base = mean(betaL_fk6)

preserve
collapse  (mean) L_e*, by(fk period)
keep fk period *_5y
save "`data'PF_Labor_Elasticity_BLS_5y.dta", replace
restore

preserve
collapse  (mean) L_e*, by(fk dec)
keep fk dec *_10y
save "`data'PF_Labor_Elasticity_BLS_10y.dta", replace
restore

preserve
collapse  (mean) L_e*, by(fk)
keep fk *_base
save "`data'PF_Labor_Elasticity_BLS_base.dta", replace
restore

/************************************************************
FK4: Import labor elasticities with BLS benefits adjustments
*************************************************************/
use "`data'PF_FK4_Year_BetaL.dta", clear

gen period = 1 if year<=1982
	replace period = 2 if year>1982 & year<=1987
	replace period = 3 if year>1987 & year<=1992
	replace period = 4 if year>1992 & year<=1997
	replace period = 5 if year>1997 & year<=2002
	replace period = 6 if year>2002 & year<=2007
	
bys naics4 period: egen L_elasticity_BLS_5y = mean(betaL_fk4)

preserve
collapse  (mean) L_e*, by(naics4 period)
keep naics4 period *_5y
save "`data'PF_Labor_Elasticity_BLS_5y_fk4.dta", replace
restore




}




/*********************************************************
SECTION 5: 
	- Gross Output Parameters
**********************************************************/
if `S5' == 1 {
/*Estimated GO parameters*/
use "`boots'estimation_pairs_GO_ky_LP.dta", clear

gen double sigma_lp_go = 1/by_lp3
gen double mu_lp_go = sigma_lp_go/(sigma_lp_go-1)

gen double aL_lp_vrts_go = bl*mu_lp_go
gen double aM_lp_vrts_go = bm*mu_lp_go
gen double aK_lp_vrts_go = bk_lp3*mu_lp_go

gen double rts_lp_go = aL_lp_vrts_go+aM_lp_vrts_go+aK_lp_vrts_go

gen double aL_lp_crts_go = aL_lp_vrts_go/rts_lp_go
gen double aM_lp_crts_go = aM_lp_vrts_go/rts_lp_go
gen double aK_lp_crts_go = aK_lp_vrts_go/rts_lp_go

keep fk period estimation_pair *_lp*_go

tempfile lp_parms
	save "`lp_parms'"
	
/*Implied GO parameters from VA parameters*/
*Extract intermediate input shares and VA-GO ratio from earlier data
use "`boots'estimation_dataset_v1.dta", clear
egen tfp = tag(fk period)
keep if tfp==1

gen double va_tvs = betaL_go/betaL_va

keep fk_naics02 period betaM_go va_tvs
	tempfile bm_share
	save "`bm_share'"

use "`data'PF_FK_ACF.dta", clear
keep fk period *1acf_vrts_5y

merge 1:1 fk period using "`bm_share'"
	drop _m

gen double mu_va_go = mu_1acf_vrts_5y/(1-betaM_go+mu_1acf_vrts_5y*betaM_go)
gen double sigma_va_go = mu_va_go/(mu_va_go-1)

gen double aL_va_vrts_go = mu_va_go*aL_1acf_vrts_5y/mu_1acf_vrts_5y*va_tvs
gen double aK_va_vrts_go = mu_va_go*aK_1acf_vrts_5y/mu_1acf_vrts_5y*va_tvs
gen double aM_va_vrts_go = mu_va_go*betaM_go

gen rts_va_go = aL_va_vrts_go+aK_va_vrts_go+aM_va_vrts_go

gen double aL_va_crts_go = aL_va_vrts_go/rts_va_go
gen double aM_va_crts_go = aM_va_vrts_go/rts_va_go
gen double aK_va_crts_go = aK_va_vrts_go/rts_va_go

keep fk period *_va*_go

merge 1:1 fk period using "`lp_parms'"
	drop _m

/*Additional Sigmas*/
gen sigma_6 = 6
gen sigma_4 = 4
gen sigma_3 = 3

sort fk period
save "`data'PF_FK_GO.dta", replace


}


/*End of File*/
